package com.fuyin.schedule;

import com.fuyin.dao.OperationLogDao;
import com.fuyin.entity.OperationLog;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author yuanxilan (yuanxilan@keep.com)
 * @Description
 * @date 2021年11月30日 9:33 下午
 * @since
 */
@Slf4j
@Component
public class OperationLogHandler {

    @Autowired
    private OperationLogDao operationLogDao;

    @XxlJob("deleteOperationLog")
    public ReturnT<String> deleteOperationLog(String param) throws Exception {
        log.info("{} deleteOperationLog Begin", new Date());
        int period = 30;
        if (StringUtils.isNotBlank(param)) {
            try {
                period = Integer.parseInt(param);
            } catch (Exception e) {
                log.error("com.fuyin.schedule.OperationLogHandler.deleteOperationLog param:{}", param);
                XxlJobHelper.log("填写的参数必须为整数");
                return ReturnT.FAIL;
            }
        }
        operationLogDao.deleteOperationLog(period);
        log.info("{} deleteOperationLog End", new Date());
        return ReturnT.SUCCESS;
    }

    @XxlJob("getAllOperationLog")
    public ReturnT<String> getAllOperationLog(String param) throws Exception {
        log.info("{} getAllOperationLog Begin", new Date());
        XxlJobHelper.log("1234567890");
        List<OperationLog> operationLogs = operationLogDao.getOperationLogs();
        for (OperationLog operationLog : operationLogs) {
            log.info("{}", operationLog);
        }
        log.info("{} getAllOperationLog End", new Date());
        return ReturnT.SUCCESS;
    }
}
